package cn.zhj.pluginappdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.qihoo360.replugin.RePlugin;
import com.qihoo360.replugin.model.PluginInfo;

import java.text.ParseException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button install , open, uninstall;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        install = findViewById(R.id.btn_install);
        open = findViewById(R.id.btn_open);
        uninstall = findViewById(R.id.btn_uninstall);

        install.setOnClickListener(this);
        open.setOnClickListener(this);
        uninstall.setOnClickListener(this);
        requestSomePerMissions();
    }

    /**
     * 这里获取最基本权限
     * sd卡读写
     * 位置信息
     * 手机状态
     * 3个权限组弹3次框
     * 这3个权限任意不给的话还是别用这个app了。
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @SuppressLint("CheckResult")
    private void requestSomePerMissions() {
        //1.检测权限
        XXPermissions.with(this)
                .permission(Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                .permission(Permission.REQUEST_INSTALL_PACKAGES)
                .permission(Permission.SYSTEM_ALERT_WINDOW)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                        } else {
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_install:
                startInstall();
                break;
            case R.id.btn_open:
                openPluginApp();
                break;
            case R.id.btn_uninstall:
                startUninstall();
                break;

            default:
                break;
        }
    }

    /**
     * 建议 阅读下代码：com.qihoo360.replugin.packages.PluginManagerServer
     * 卸载插件逻辑：
     *
     * 先判断插件是否正在运行？
     *
     * **是：**记录“卸载状态”，推迟到到常驻进程重启的时执行卸载，此时返回 false(因为本次只记录了状态，并没有真正卸载)
     *
     * **否：**直接卸载，此时返回 true
     */
    private void startUninstall() {
        if (RePlugin.isPluginInstalled("app1")) {
            //卸载只记录状态，下次重启时卸载
            Toast.makeText(this , "开始卸载插件" , Toast.LENGTH_SHORT).show();
            RePlugin.uninstall("app1");
        }else {
            Toast.makeText(this , "未安装插件怎么卸载" , Toast.LENGTH_SHORT).show();
        }
    }

    private void openPluginApp() {
        if (RePlugin.isPluginInstalled("app1")) {
            RePlugin.startActivity(MainActivity.this, RePlugin.createIntent("cn.zhj.pluginapp1", "cn.zhj.pluginapp1.MainActivity"));
        }else {
            Toast.makeText(this , "未安装插件请先安装" , Toast.LENGTH_SHORT).show();
        }
    }

    private void startInstall() {
        new Thread(){
            @Override
            public void run() {
                if (RePlugin.isPluginInstalled("app1")) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this , "已经安装了插件了，不需要再安装" , Toast.LENGTH_SHORT).show();
                        }
                    });

                    return;
                }
                PluginInfo pi = RePlugin.install("/sdcard/MT2/apks/pluginapp1-debug.apk");
                if (pi != null) {
//                    RePlugin.preload(pi);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this , "插件安装成功 activity" , Toast.LENGTH_SHORT).show();
                        }
                    });
                }else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this , "插件安装失败 activity" , Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        }.start();

    }
}